package com.atguigu.juc.chap01;


public class Lock8 {

    public static void main(String[] args) {

        // 0.
        /*new Thread(new T100(), "A").start();
        new Thread(new T100(), "B").start();*/


        // 1. 先访问短信,再访问邮件,先打印短信还是邮件,先短信再邮件！
        /*Phone phone = new Phone();
        new Thread(()->{
            //System.out.println(Thread.currentThread().getName() + "is Running");
            phone.sendSMS();
        }, "A").start();
        new Thread(()->{
            //System.out.println(Thread.currentThread().getName() + "is Running");
            phone.sendEmail();
        }, "B").start();*/

        // 2. 在短信方法内睡眠4秒,先访问短信,再访问邮件,先打印短信还是邮件,先短信再邮件！
        /*Phone phone = new Phone();
        new Thread(()->{
            phone.sendSMS();
        }).start();
        new Thread(()->{
            phone.sendEmail();
        }).start();*/

        // 3. 在短信方法内睡眠4秒,先访问短信，再访问hello方法，是先打短信还是hello
        /*Phone phone = new Phone();
        new Thread(()->{
            phone.sendSMS();
        }).start();
        new Thread(()->{
            phone.hello();
        }).start();*/


        // 4. 在短信方法内睡眠4秒,两部手机，第一部发短信，第二部发邮件，先打印短信还是邮件
        /*Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            phone1.sendSMS();
        }).start();
        new Thread(()->{
            phone2.sendEmail();
        }).start();*/

        // 5. 在短信方法内睡眠4秒,两个静态同步方法，1部手机，先打印短信还是邮件
        /*Phone phone = new Phone();
        new Thread(()->{
            phone.sendSMS();
        }).start();
        new Thread(()->{
            phone.sendEmail();
        }).start();*/

        // 6. 在短信方法内睡眠4秒,两个静态同步方法，2部手机，先打印短信还是邮件
        // 锁的是类对象，虽然有两部手机，但是依然是一把锁
    /*Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            phone1.sendSMS();
        }).start();
        new Thread(()->{
            phone2.sendEmail();
        }).start();*/

        // 7. 在短信方法内睡眠4秒,1个静态同步方法，1个普通同步方法，1部手机，先打印短信还是邮件
        /*Phone phone = new Phone();
        new Thread(()->{
            phone.sendSMS();//static  class对象
        }).start();
        new Thread(()->{
            phone.sendEmail();  //this
        }).start();*/


        // 8. 在短信方法内睡眠4秒,1个静态同步方法，1个普通同步方法，2部手机，先打印短信还是邮件
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(() -> {
            phone1.sendSMS(); // static  class
        }).start();
        new Thread(() -> {
            phone2.sendEmail(); // this
        }).start();
    }
}

class Phone {

    /*public static synchronized void sendSMS() {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("sendSMS");
    }*/
    public static void sendSMS() {
        synchronized (Phone.class) {
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("sendSMS");
        }
    }


    /*public synchronized void sendEmail() {
        System.out.println("sendEmail");
    }*/
    public void sendEmail() {
        synchronized (this) {
            System.out.println("sendEmail");
        }
    }

    public void hello() {
        System.out.println("hello");
    }
}

class T100 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "is Running");
    }
}

class T200 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "is Running");
    }
}
